package thesis.ramstorage.core;

import java.util.ArrayList;
import java.util.Collection;

import thesis.ramstorage.application.DataObj;

public class VirtualNode extends Node {

    private PhysicalNode pNode;
    private Long ID;
    private int index; //index in physicalNode
    private Long capacity = 1L;
    private boolean heavyLoad = false;
    private boolean claimOverload = false;

//    private Long load;
    public VirtualNode() {
        super();
        this.state = Fallible.OK;
    }

    public long getID() {
        // TODO Auto-generated method stub
        return this.ID;
    }

    @Override
    public Protocol getProtocol(int i) {
        // TODO Auto-generated method stub
        return null;
    }

    public int protocolSize() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setID(Long index) {
        // TODO Auto-generated method stub
        this.ID = index;
    }

    public int getFailState() {
        // TODO Auto-generated method stub
        return this.state;
    }

    public void setFailState(int failState) {
        // TODO Auto-generated method stub
        this.state = failState;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int i) {
        this.index = i;
    }

    public synchronized String put(long id, DataObj dataObj) throws Exception {
        // TODO Auto-generated method stub
        //		PhysicalNode pNode =
        //		if(this.getLoad() / getCapacity() >  SystemConfiguration.overLoadClaim ){
        //			//heavyload claim
        ////			System.out.println("Node of " + this.getPhysicalNodeId() + " is heavy load");
        ////			System.out.println("New server is joined...");
        //			this.heavyLoad = true;
        ////			return;
        //		}
        if (this.getLoad() - getCapacity() == 0) {
            if (claimOverload) {
                //				overload claim if load over capacity%
                setFailState(Fallible.OVERLOAD);
//                throw new Exception("virtual node of " + this.getPhysicalNodeId() + " is overload. can not input data!");
            }
        }

        this.pNode.put(this.ID, id, dataObj);
        //		if(NetworkControl.allNodeLoad[this.getPhysicalNodeId().intValue()] > )
        //		NetworkControl.allNodeLoad[this.getPhysicalNodeId().intValue()] += getLoad();
        return "";

    }

    public void run() {
        // TODO Auto-generated method stub
    }

    public synchronized DataObj get(Long id) {
        // TODO Auto-generated method stub
        return this.pNode.get(this.ID, id);
    }

    public synchronized int getLoad() {
        // TODO Auto-generated method stub
//        return this.pNode.get.size();//need to reimplement
        return 0;
    }

    public synchronized Long getCapacity() {
        return capacity;
    }

    public void setCapacity(Long capacity) {
        this.capacity = capacity;
    }

    public boolean isHeavyload() {
        return heavyLoad;
    }

    public void flushData() {
        // TODO Auto-generated method stub
//        this.store.clear();
    }

    public synchronized Collection<? extends Long> listKeys() {
        // TODO Auto-generated method stub
        return new ArrayList<Long>(this.pNode.listKeys(this.ID));
    }

    public synchronized void remove(Long storedKey) {
        // TODO Auto-generated method stub
        this.pNode.remove(this.ID, storedKey);
    }

    public String printOut() {
        // TODO Auto-generated method stub
        return this.ID.toString();
    }

    public int compareTo(Node arg0) {
        // TODO Auto-generated method stub
        return this.getID() <= arg0.getID() ? 1 : -1;
    }

    public PhysicalNode getpNode() {
        return pNode;
    }

    public void setpNode(PhysicalNode pNode) {
        this.pNode = pNode;
    }
    
}
